package com.example.swim.Service;


import com.example.swim.Dao.StudentDao;
import com.example.swim.Pojo.Student;


import com.example.swim.Utils.R;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.Iterator;
import java.util.List;

/**
 * @author LiYa
 * @create 2022-07-17 19:50
 */
@Transactional
@Service
public class StudentService  {
    @Autowired
    StudentDao studentDao;

    public Student selectById(int id){
        return studentDao.selectById(id);
    }
    public List<Student> findAll()
    {
        return studentDao.findAll();
    }
    public void insert(Student student)
    {
        studentDao.insertStudent(student);
    }
    public void del(Integer id)
    {
        studentDao.delStudent(id);
    }
    public void update(Student stu)
    {
        studentDao.updateStudent(stu);
    }
    public Student findStu(Student student)
    {
        return studentDao.findStu(student);
    }
    public void insertNew(Student student)
    {
        studentDao.insertNew(student);
        System.out.println("成功报名进入一个学生");
    }
    public  Integer checkFirst(Student student)
    {
        return studentDao.checkFirst(student);
    }
    public void updateFirst(Student student)
    {
        studentDao.updateFirst(student);
    }
    public void updatePwd(Student student)
    {
        studentDao.updatePwd(student);
        System.out.println("学员修改密码成功");
    }
    public void addAid(Integer aid,Integer id)
    {
        studentDao.addAid(aid,id);
        System.out.println("学员报名活动成功");
    }
    public Student quitA(Student student){
       student.setAid(null);
       studentDao.updateStudent(student);
       return  student;
    }
    public R Check(int id,int cid){
        Student stu=studentDao.selectById(id);
        if(stu.getCid()!=null)
        {
            return new R(false, "学员已报名课程，需要退选课程才可以报名");
        }
        stu.setCid(cid);
        studentDao.updateStudent(stu);
        return new R(true,"学员报名成功");
    }
    public void importExcel(MultipartFile file)
    {
        InputStream in=null;
        try
        {
//            得到Excel文件的输入流
            in= file.getInputStream();
//            获取excel文件对象
            Workbook workbook=null;
//            根据后缀得到不同的Workbook子类，即HSSFWorkbook或XSSFWorkbook
            if(file.getOriginalFilename().endsWith("xlsx"))
            {
                workbook=new XSSFWorkbook(in);
            }
            else if(file.getOriginalFilename().endsWith("xls"))
            {
                workbook=new HSSFWorkbook(in);
            }
            else
            {
                throw new Exception("文件格式不对或者文件损坏");
            }
//            获得sheet对应的对象，获取第一页对象
            Sheet sheet=workbook.getSheetAt(0);
//            创建Station对象容器
            Student stu;
//            解析sheet，获得多行数据，并放入迭代器中
            Iterator<Row> ito=sheet.iterator();
            int count=0;
            int sum=0;
            Row row=null;
//            每次导入就把上次的删除
//            delInterfaceList();
            while(ito.hasNext())
            {
                row=ito.next();
//                由于第一行是标题，因此这里需要单独处理
                if(count==0)
                {
                    ++count;
                    continue;
                }
                else
                {
                    if(row!=null)
                    {
                        String name=ExcelUtils.getCellValue((Cell) row.getCell(0));
                        int age=Integer.parseInt(ExcelUtils.getCellValue((Cell) row.getCell(1)));
                        String phone=ExcelUtils.getCellValue((Cell) row.getCell(2));
                        String e_mail=ExcelUtils.getCellValue((Cell) row.getCell(3));
                        int cid=Integer.parseInt(ExcelUtils.getCellValue((Cell) row.getCell(4)));
                        int aid=Integer.parseInt(ExcelUtils.getCellValue((Cell) row.getCell(5)));
                        String password=ExcelUtils.getCellValue((Cell) row.getCell(6));
                        stu=new Student(name,age,phone,e_mail,cid,aid,password,0);
                        System.out.println(stu.toString());
                        insert(stu);
                    }
                }
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
